﻿using GrayParrot.Communications.SignalR;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.Communications.SignalR
{
    /// <summary>
    /// Signalr generic Hub base class...
    /// </summary>
    public class HubBase<T> : Hub where T : IHub
    {
        static IHubContext _context = null;

        private IDisposable m_signalR { get; set; }

        /// <summary>
        /// Context instance to access client connections to broadcast to
        /// </summary>
        public static IHubContext HubContext
        {
            get
            {
                if (_context == null)
                    _context = GlobalHost.ConnectionManager.GetHubContext<T>();

                return _context;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HubBase{T}"/> class.
        /// </summary>
        public HubBase()
        {
        }

        /// <summary>
        /// Inits the specified service.
        /// </summary>
        /// <param name="urlEndpoint">The URL endpoint.</param>
        /// <returns></returns>
        //public IDisposable Init(string urlEndpoint)
        //{
        //    try
        //    {
        //        //m_signalR = WebApp.Start<SignalRStartup>(urlEndpoint);
        //        m_signalR = WebApp.Start(urlEndpoint);
        //    }
        //    catch (Exception ex)
        //    {

        //        GrayParrot.Logging.Logger.For<HubBase<T>>().Error("Collegamento a SignalR fallito !!", ex);
        //    }

        //    return m_signalR;
        //}
    }
}
